<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Users extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model(array('admin_model', 'admin_group_model', 'users_model'));
		$this->config->load('agent_level', true);
	}
	
	public function index($opt = '')
	{
		$levels = $this->config->item('levels', 'agent_level');
		$data   = array('cur' => 'users_index', 'levels' => $levels);
		$this->load->vars($data);
		$row    = $this->admin_model->is_login('admin/login');

		//权限检查
		if(!$this->admin_group_model->check_perm('users_index'))
		{
			show_404();
			return;
		}
		
		if($opt == 'edit')
		{
			$user_id   = (integer)$this->input->get_post('id');
			$user_info = $this->users_model->get_by_id($user_id);
			if(empty($user_info))
			{
				show_404();
				return;
			}
			
			if($_SERVER['REQUEST_METHOD']=='POST')
			{
				$username  = $this->input->get_post('username');
				$mobile    = $this->input->get_post('mobile');
				$fullname  = $this->input->get_post('fullname');
				$password  = $this->input->get_post('password');
				$email     = $this->input->get_post('email');
				$qq        = $this->input->get_post('qq');
				if(empty($username))
				{
					echo '<script>alert("请填写登录帐号!")</script>';
					return;	
				}
				if(empty($mobile))
				{
					echo '<script>alert("请填写手机号!")</script>';
					return;
				}
				if(!preg_match("/^1(3|4|5|7|8)\d{9}$/", $mobile) || strlen($mobile) != 11)
				{
					echo '<script>alert("无效的手机号!")</script>';
					return;
				}
				if(empty($email))
				{
					echo '<script>alert("请填写邮箱!")</script>';
					return;
				}
				$check_name = $this->users_model->get_by_where(array('username'=>$username));
				if($check_name && $check_name['user_id'] != $user_id && $check_name['status']<>-1)
				{
					echo '<script>alert("登录帐号已被使用!")</script>';
					return;	
				}
				$check_name = $this->users_model->get_by_where(array('mobile'=>$mobile));
				if($check_name && $check_name['user_id'] != $user_id && $check_name['status']<>-1)
				{
					echo '<script>alert("手机号已被使用!")</script>';
					return;	
				}
				$check_name = $this->users_model->get_by_where(array('email'=>$email));
				if($check_name && $check_name['user_id'] != $user_id && $check_name['status']<>-1)
				{
					echo '<script>alert("邮箱已被使用!")</script>';
					return;	
				}
				$update = array(
					'username' => $username,
					'mobile'   => $mobile,
					'fullname' => $fullname,
					'qq'       => $qq,
					'email'    => $email,
				);
				if($password)
				{
					$salt               = randomstr(6);
					$password           = md5(md5($password).$salt);
					$update['password'] = $password;
					$update['salt']     = $salt;
				}

				$this->users_model->update($user_id, $update);
				
				echo '<script>alert("更新成功!"); parent.window.location.reload(true);</script>';
				return;
			}
			
			$data   = array(
				'user' => $row,
				'info' => $user_info,
			);
			$this->load->view('admin/users_edit', $data);
			return;
		}
		
		if($opt == 'status')
		{
			$admin_id   = (integer)$this->input->get_post('id');
			$update     = array(
				'status' => intval($this->input->get_post('val')),
			);
			$this->users_model->update($admin_id, $update);
			$data = array(
				'error' => '',
				'msg' => '操作成功!',
			);
			echo json_encode($data);
			return;
		}
		
        $base_url   = site_url('admin/users/index');
        $per_page   = 20;
        $cur_page   = (integer)$this->uri->segment(4);
        $cur_page   = $cur_page > 0 ? $cur_page : 1;
        $offset     = ($cur_page - 1) * $per_page;

		$q			= (string)$this->input->get('q');

		$where      = "where `status`<>-1";
		
		if(!empty($q))
		{
			$where  .= " and (`username` like '%".$q."%' or `mobile` like '%".$q."%')";
		}
		
		$order_by   = "order by `regtime` desc";
		$result     = $this->users_model->result_data($where, $order_by, $offset, $per_page);
		$total      = $this->users_model->result_count($where);
		
		$data   = array(
			'user' => $row,
			'result' => $result,
			'total' => $total,
			'page_html' => create_page_links($base_url, $total, $per_page, $cur_page, '', ''),
		);
		$this->load->view('admin/users_index', $data);
	}
}